package com.rainsoft.center.isec.stream.library.service;

import com.rainsoft.center.isec.stream.library.entity.hbase.impl.RealidLoginout;
import org.apache.commons.collections.IteratorUtils;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;

import java.util.*;

/**
 * @Name com.rainsoft.center.isec.stream.library.service.UDB
 * @Description
 * @Author Elwyn
 * @Version 2017/12/1
 * @Copyright 上海云辰信息科技有限公司
 **/
public class UDB {
	void saveMac(JavaRDD<RealidLoginout> realidLoginoutJavaRDD) {

		realidLoginoutJavaRDD.mapToPair(new PairFunction<RealidLoginout, String,
				RealidLoginout>() {
			@Override
			public Tuple2<String, RealidLoginout> call(RealidLoginout realidLoginout) throws Exception {
				return new Tuple2<>(realidLoginout.getIdcode(), realidLoginout);
			}
		}).groupByKey().flatMap(new FlatMapFunction<Tuple2<String, Iterable<RealidLoginout>>, RealidLoginout>() {
			@Override
			public Iterator<RealidLoginout> call(Tuple2<String, Iterable<RealidLoginout>> tuple2) throws Exception {
				Iterable<RealidLoginout> realidLoginouts = tuple2._2;
				List<RealidLoginout> list = IteratorUtils.toList(realidLoginouts.iterator());
				RealidLoginout max = Collections.max(list, new Comparator<RealidLoginout>() {
					@Override
					public int compare(RealidLoginout o1, RealidLoginout o2) {
						return 0;
					}
				});
				List<RealidLoginout> realidLoginouts1 = new ArrayList<>();

				realidLoginouts1.add(max);


				return null;
			}
		}).collect();


		realidLoginoutJavaRDD.groupBy(new Function<RealidLoginout, Object>() {
			@Override
			public Object call(RealidLoginout v1) throws Exception {
				return null;
			}
		});

	}
}
